<?
	include_once("includes/adodb/adodb.inc.php");
	
	function validateUser()
	{
		include_once("includes/mcrypt.php");
		include_once("includes/constants.php");
		
		try
		{
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			$rsUser = $DB->Execute("SELECT * FROM users WHERE userState <> '$stateDeleted' AND userName = '".$_REQUEST["userName"]."'");
			if($rsUser->EOF)
			{
				$userMesage = array("permission" => "DENIED");
				return json_encode($userMesage);
			}
			else
			{
				if(date("Ymd", strtotime($rsUser->fields("expirationDate"))) < date("Ymd")) {
					$userMesage = array("permission" => "DENIED");
					return json_encode($userMesage);
				}
				
				$mcrypt = new MCrypt();
				if($mcrypt->decrypt($rsUser->fields("userPassword")) == $mcrypt->decrypt($_REQUEST["userPassword"]))
				{
					$userMesage = array("permission" => "GRANT",
										"idUser" => $rsUser->fields("idUser"),
										"token" => $mcrypt->encrypt($rsUser->fields("expirationDate")."|".$rsUser->fields("userCompleteName")),
										"userName" => $rsUser->fields("userName"),
										"userCompleteName" => $rsUser->fields("userCompleteName"),
										"userEMail" => $rsUser->fields("userEMail")
									   );
					return json_encode($userMesage);
				}
				else
				{
					$userMesage = array("permission" => "DENIED");
					return json_encode($userMesage);
				}
			}
		}
		catch (Exception $e)
		{
			return json_encode(array("permission" => "DENIED"));
		}
	}
	
	function getMeasurements()
	{
		include_once("includes/mcrypt.php");
		include_once("includes/constants.php");
		
		try
		{
			$mcrypt = new MCrypt();
		
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			$strSQL = "SELECT * FROM measurement WHERE idUser = ".$_REQUEST["idUser"];
			$rsMeasurement = $DB->Execute($strSQL);
				
			$i = 0;
			if($rsMeasurement->EOF)
				return json_encode(array());
			else {
				while(!$rsMeasurement->EOF)
				{
					$arrJSON[$i]['idMeasurement'] = $rsMeasurement->fields("idMeasurement");
					$arrJSON[$i]['dateMeasurement'] = $rsMeasurement->fields("dateMeasurement");
					$arrJSON[$i]['idTrack'] = $rsMeasurement->fields("idTrack");
					$arrJSON[$i]['idTemperature'] = $rsMeasurement->fields("idTemperature");
					$arrJSON[$i]['idHumidity'] = $rsMeasurement->fields("idHumidity");
					$arrJSON[$i]['idUser'] = $rsMeasurement->fields("idUser");
					
					$sqlQuery = "SELECT * FROM measurement_item WHERE idMeasurement = " . $rsMeasurement->fields("idMeasurement");
					$rsMeasurementItem = $DB->Execute($sqlQuery);
					
					if($rsMeasurementItem->EOF) {
						$arrJSON[$i]['items'] = array();
					}
					else {
						$j = 0;
						while(!$rsMeasurementItem->EOF)
						{
							$arrJSON[$i]['items'][$j]['idMeasurementItem'] = $rsMeasurementItem->fields("idMeasurementItem");
							$arrJSON[$i]['items'][$j]['idMeasurement'] = $rsMeasurementItem->fields("idMeasurement");
							$arrJSON[$i]['items'][$j]['idHorse'] = $rsMeasurementItem->fields("idHorse");
							$arrJSON[$i]['items'][$j]['initTime'] = $rsMeasurementItem->fields("initTime");
							$arrJSON[$i]['items'][$j]['finishTime'] = $rsMeasurementItem->fields("finishTime");
							$arrJSON[$i]['items'][$j]['lacticAcid1'] = $rsMeasurementItem->fields("lacticAcid1");
							$arrJSON[$i]['items'][$j]['heartRate1'] = $rsMeasurementItem->fields("heartRate1");
							$arrJSON[$i]['items'][$j]['lacticAcid2'] = $rsMeasurementItem->fields("lacticAcid2");
							$arrJSON[$i]['items'][$j]['heartRate2'] = $rsMeasurementItem->fields("heartRate2");
							$arrJSON[$i]['items'][$j]['lacticAcid3'] = $rsMeasurementItem->fields("lacticAcid3");
							$arrJSON[$i]['items'][$j]['heartRate3'] = $rsMeasurementItem->fields("heartRate3");
							$arrJSON[$i]['items'][$j]['respiratoryRate3'] = $rsMeasurementItem->fields("respiratoryRate3");
							$arrJSON[$i]['items'][$j]['idWork'] = $rsMeasurementItem->fields("idWork");
							$arrJSON[$i]['items'][$j]['meters'] = $rsMeasurementItem->fields("meters");
							$arrJSON[$i]['items'][$j]['time'] = $rsMeasurementItem->fields("time");
							$arrJSON[$i]['items'][$j++]['resultValues'] = $rsMeasurementItem->fields("resultValues");
							
							$rsMeasurementItem->MoveNext();
						}
					}
					
					$rsMeasurement->MoveNext();
					$i++;
				}
			}
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			return json_encode(array());
		}
	}
	
	function getHorses()
	{
		include_once("includes/mcrypt.php");
		include_once("includes/constants.php");
		
		try
		{
			$mcrypt = new MCrypt();
		
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			$strSQL = "SELECT * FROM horses h ".
					  "WHERE h.state <> '$stateDeleted'".
					  " AND h.idUser = ".$_REQUEST["idUser"];

			$rsHorses = $DB->Execute($strSQL);
			
			$i = 0;
			if($rsHorses->EOF)
				return json_encode(array());
			else {
				while(!$rsHorses->EOF)
				{
					$arrJSON[$i]['idHorse'] = $rsHorses->fields("idHorse");
					$arrJSON[$i]['name'] = $mcrypt->encrypt($rsHorses->fields("name"));
					$arrJSON[$i]['yearsOld'] = $mcrypt->encrypt($rsHorses->fields("yearsOld"));
					$arrJSON[$i]['hair'] = $mcrypt->encrypt($rsHorses->fields("hair"));
					$arrJSON[$i]['temperament'] = $mcrypt->encrypt($rsHorses->fields("temperament"));
					$arrJSON[$i]['state'] = $rsHorses->fields("state");
					$arrJSON[$i]['idUser'] = $rsHorses->fields("idUser");
					
					$sqlQuery = "SELECT * FROM horses_racing WHERE idHorse = " . $rsHorses->fields("idHorse");
					$rsRacing = $DB->Execute($sqlQuery);
					
					if($rsRacing->EOF) {
						$arrJSON[$i]['racing'] = array();
					}
					else {
						$j = 0;
						while(!$rsRacing->EOF)
						{
							$arrJSON[$i]['racing'][$j]['idHorseRacing'] = $rsRacing->fields("idHorseRacing");
							$arrJSON[$i]['racing'][$j]['dateRacing'] = $mcrypt->encrypt($rsRacing->fields("dateRacing"));
							$arrJSON[$i]['racing'][$j++]['distance'] = $mcrypt->encrypt($rsRacing->fields("distance"));
							
							$rsRacing->MoveNext();
						}
					}
					
					$rsHorses->MoveNext();
					$i++;
				}
			}
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			return json_encode(array());
		}
	}
	
	function setHorses()
	{
		include_once("includes/mcrypt.php");
		include_once("includes/constants.php");
		
		try
		{
			$mcrypt = new MCrypt();
		
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			$jaHorses = json_decode($_REQUEST['jaHorses'], true);
			
			$DB->StartTrans(); // start transaction
			
			$arrResponse = array();
			foreach ($jaHorses as $joHorse) 
			{
			
				$strSQL = "INSERT INTO horses ( ";
				$strSQL .= "	name, ";
				$strSQL .= "	yearsOld, ";
				$strSQL .= "	hair, ";
				$strSQL .= "	pretreatments, ";
				$strSQL .= "	temperament, ";
				$strSQL .= "	state, ";
				$strSQL .= "	idUser ";
				$strSQL .= "	) VALUES ( ";
				$strSQL .= "	'".mb_strtoupper($mcrypt->decrypt($joHorse['name']), "UTF-8")."', ";
				$strSQL .= "	'".$mcrypt->decrypt($joHorse['yearsOld'])."', ";
				$strSQL .= "	'".mb_strtoupper($mcrypt->decrypt($joHorse['hair']), "UTF-8")."', ";
				$strSQL .= "	'', ";
				$strSQL .= "	'".mb_strtoupper($mcrypt->decrypt($joHorse['temperament']), "UTF-8")."', ";
				$strSQL .= "	'".$stateActive."', ";
				$strSQL .= "	".$joHorse['idUser'];
				$strSQL .= "	)";

				$rsHorses = $DB->Execute($strSQL);
				
				$objResponse = "";
				$objResponse->idHorse = $joHorse['idHorse'];
				$objResponse->idWeb = $DB->Insert_ID();
				
				array_push($arrResponse, $objResponse);
			}
			
			$arrJSON->status = "OK";
			$arrJSON->data = $arrResponse;
			
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->msg = "Error al Grabar el/los Caballo/s";
			return json_encode($arrJSON);
		}
	}
	
	function uploadMeasurement()
	{
		include_once("includes/constants.php");
		
		try
		{
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
				
			$joMeasurement = json_decode($_REQUEST['joMeasurement'], true);
				
			$DB->StartTrans(); // start transaction

			$strSQL = "INSERT INTO measurement ( ";
			$strSQL .= "	dateMeasurement, ";
			$strSQL .= "	idTrack, ";
			$strSQL .= "	idTemperature, ";
			$strSQL .= "	idHumidity, ";
			$strSQL .= "	idUser ";
			$strSQL .= "	) VALUES ( ";
			$strSQL .= "	'".$joMeasurement['dateMeasurement']."', ";
			$strSQL .= "	".$joMeasurement['idTrack'].", ";
			$strSQL .= "	".$joMeasurement['idTemperature'].", ";
			$strSQL .= "	".$joMeasurement['idHumidity'].", ";
			$strSQL .= "	".$joMeasurement['idUser']." ";
			$strSQL .= "	)";
			
			$rsMesurement = $DB->Execute($strSQL);
			
			$measurement_ID = $DB->Insert_ID();
			
			$arrResponse = array();
			foreach ($joMeasurement['items'] as $joItem)
			{
				$strSQL = "INSERT INTO measurement_item ( ";
				$strSQL .= "	idMeasurement, ";
				$strSQL .= "	idHorse, ";
				$strSQL .= "	initTime, ";
				$strSQL .= "	finishTime, ";
				$strSQL .= "	lacticAcid1, ";
				$strSQL .= "	heartRate1, ";
				$strSQL .= "	lacticAcid2, ";
				$strSQL .= "	heartRate2, ";
				$strSQL .= "	lacticAcid3, ";
				$strSQL .= "	heartRate3, ";
				$strSQL .= "	respiratoryRate3, ";
				$strSQL .= "	idWork, ";
				$strSQL .= "	meters, ";
				$strSQL .= "	time ";
				$strSQL .= "	) VALUES ( ";
				$strSQL .= "	".$measurement_ID.", ";
				$strSQL .= "	 ".$joItem['idHorse'].", ";
				$strSQL .= "	'".$joItem['initTime']."', ";
				$strSQL .= "	'".$joItem['finishTime']."', ";
				$strSQL .= "	'".$joItem['lacticAcid1']."', ";
				$strSQL .= "	'".$joItem['heartRate1']."', ";
				$strSQL .= "	'".$joItem['lacticAcid2']."', ";
				$strSQL .= "	'".$joItem['heartRate2']."', ";
				$strSQL .= "	'".$joItem['lacticAcid3']."', ";
				$strSQL .= "	'".$joItem['heartRate3']."', ";
				$strSQL .= "	'".$joItem['respiratoryRate3']."', ";
				$strSQL .= "	 ".$joItem['idWork'].", ";
				$strSQL .= "	'".$joItem['meters']."', ";
				$strSQL .= "	'".$joItem['time']."' ";
				$strSQL .= "	)";
		
				$DB->Execute($strSQL);
				
				$measurementItem_ID = $DB->Insert_ID();
				
				$objResult = getResultValues($measurementItem_ID);
				
				array_push($arrResponse, $objResult);
			}
			
			if (!$DB->CompleteTrans())
			{
				$arrJSON->status = "ERROR";
				$arrJSON->idMeasurementWeb = $measurement_ID;
				$arrJSON->data = array();
			}
			else
			{
				$arrJSON->status = "OK";
				$arrJSON->idMeasurementWeb = $measurement_ID;
				$arrJSON->data = $arrResponse;
			}
			
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->idMeasurementWeb = 0;
			$arrJSON->data = array();
			
			return json_encode($arrJSON);
		}
	}
	
	function getResultValues($idMeasurementItem = "")
	{
		require("includes/constants.php");
		
		switch ($_REQUEST['language']) {
			case "es":
				require("includes/resources/resources_es.php");
				break;
			case "en":
				require("includes/resources/resources_en.php");
				break;
			default:
				require("includes/resources/resources_es.php");
				break;
		}
		
		try
		{
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			if($idMeasurementItem == "")
				$idMeasurementItem = $_REQUEST["idMeasurementItem"];
			
			$rsMeasurement = $DB->Execute("SELECT mi.* FROM measurement_item mi WHERE mi.idMeasurementItem = " . $idMeasurementItem);
			
			$objResult = "";
			if(!$rsMeasurement->EOF){
				$fc1 = $rsMeasurement->fields('heartRate1');
				$fc3 = $rsMeasurement->fields('heartRate3');
				
				switch ($fc1) {
					case ($fc1 > 130 && $fc1 < 40):
						$objResult->WorkIntensity = utf8_encode($a_languages['Measurement.Answers.FC.Error']);
						break;
					case ($fc1 > 120 && $fc1 <= 130):
						$objResult->WorkIntensity = $a_languages['Measurement.Answers.FC1.High'].". ".utf8_encode($a_languages['Measurement.Answers.FC.OutRange']);
						break;
					case ($fc1 >= 90 && $fc1 <= 120):
						$objResult->WorkIntensity = $a_languages['Measurement.Answers.FC1.High'];
						switch ($fc3) {
							case ($fc3 < 60):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Good'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Good.Recommendation']);
								break;
							case ($fc3 >= 60 && $fc3 < 70):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Medium'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Medium.Recommendation']);
								break;
							case ($fc3 >= 70 && $fc3 < 90):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Regular'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Regular.Recommendation']);
								break;
							case ($fc3 >= 90):
								$objResult->AerobicRecovery = utf8_encode($a_languages['Measurement.Answers.FC3.Patology']);
								$objResult->AerobicRecovery_Recommendation = "";
								break;
						}
						break;
					case ($fc1 >= 80 && $fc1 < 90):
						$objResult->WorkIntensity = $a_languages['Measurement.Answers.FC1.Medium'];
						switch ($fc3) {
							case ($fc3 < 60):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Good'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Good.Recommendation']);
								break;
							case ($fc3 >= 60 && $fc3 < 70):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Medium'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Medium.Recommendation']);
								break;
							case ($fc3 >= 70 && $fc3 < 90):
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Regular'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Regular.Recommendation']);
								break;
							case ($fc3 >= 90):
								$objResult->AerobicRecovery = utf8_encode($a_languages['Measurement.Answers.FC3.Patology']);
								$objResult->AerobicRecovery_Recommendation = "";
								break;
						}
						break;
					case ($fc1 >= 60 && $fc1 < 80):
						$objResult->WorkIntensity = $a_languages['Measurement.Answers.FC1.MediumDown'];
						switch ($fc3) {
							case $fc3 < 60:
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Good'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Good.Recommendation']);
								break;
							case $fc3 >= 60 && $fc3 < 70:
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Medium'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Medium.Recommendation']);
								break;
							case $fc3 >= 70 && $fc3 < 80:
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Regular'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Regular.Recommendation']);
								break;
							case $fc3 >= 80:
								$objResult->AerobicRecovery = utf8_encode($a_languages['Measurement.Answers.FC3.Patology']);
								$objResult->AerobicRecovery_Recommendation = "";
								break;
						}
						break;
					case ($fc1 >= 40 && $fc1 < 60):
						$objResult->WorkIntensity = $a_languages['Measurement.Answers.FC1.Down'];
						switch ($fc3) {
							case $fc3 < 60:
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Good'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Good.Recommendation']);
								break;
							case $fc3 >= 60 && $fc3 < 70:
								$objResult->AerobicRecovery = $a_languages['Measurement.Answers.FC3.Regular'];
								$objResult->AerobicRecovery_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC1.FC1.Regular.Recommendation']);
								break;
							case $fc3 >= 70:
								$objResult->AerobicRecovery = utf8_encode($a_languages['Measurement.Answers.FC3.Patology']);
								$objResult->AerobicRecovery_Recommendation = "";
								break;
						}
						break;
				}
				
				$lac1 = $rsMeasurement->fields('lacticAcid1');
				$lac2 = $rsMeasurement->fields('lacticAcid2');
				$lac3 = $rsMeasurement->fields('lacticAcid3');
				
				switch ($lac1) {
					case ($lac1 >= 1 && $lac1 < 4):
						$objResult->MuscularEffort = $a_languages['Measurement.Answers.LAC1.VeryLow'];
						break;
					case ($lac1 >= 4 && $lac1 < 7):
						$objResult->MuscularEffort = $a_languages['Measurement.Answers.LAC1.Low'];
						break;
					case ($lac1 >= 7 && $lac1 < 10):
						$objResult->MuscularEffort = $a_languages['Measurement.Answers.LAC1.Medium'];
						break;						
					case ($lac1 >= 10 && $lac1 < 12):
						$objResult->MuscularEffort = $a_languages['Measurement.Answers.LAC1.High'];
						break;						
					case ($lac1 >= 12 && $lac1 < 25):
						$objResult->MuscularEffort = $a_languages['Measurement.Answers.LAC1.VeryHigh'];
						break;
					default:
						$objResult->MuscularEffort = utf8_encode($a_languages['Measurement.Answers.LAC1.Error']);
						break;						
				}

				switch ($lac2) {
					case ($lac2 <= $lac1*1.8 && $lac2 >= $lac1):
						$objResult->OxygenInput = $a_languages['Measurement.Answers.LAC2.Decreased'];
						$objResult->OxygenInput_Recomedation = utf8_encode($a_languages['Measurement.Answers.LAC2.Decreased.Recommendation']);
						break;
					case ($lac2 >= $lac1*0.1 && $lac2 < $lac1):
						$objResult->OxygenInput = $a_languages['Measurement.Answers.LAC2.Normal'];
						$objResult->OxygenInput_Recomedation = "";
						break;
					default:
						$objResult->OxygenInput = $a_languages['Measurement.Answers.LAC2.Error'];
						$objResult->OxygenInput_Recomedation = "";
						break;
				}
				
				switch ($lac3) {
					case ($lac3 >= 1 && $lac3 < 5):
						$objResult->HighRiskEnzymes = $a_languages['Measurement.Answers.LAC3.Low'];
						$objResult->HighRiskEnzymes_Recommendation = "";
						break;
					case ($lac3 >= 5 && $lac3 < 8):
						$objResult->HighRiskEnzymes = $a_languages['Measurement.Answers.LAC3.Medium'];
						$objResult->HighRiskEnzymes_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC3.HighRiskEnzymes.Medium']);
						break;
					case ($lac3 >= 8):
						$objResult->HighRiskEnzymes = $a_languages['Measurement.Answers.LAC3.High'];
						$objResult->HighRiskEnzymes_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC3.HighRiskEnzymes.High']);
						break;						
					default:
						$objResult->HighRiskEnzymes = $a_languages['Measurement.Answers.LAC3.Error'];
						$objResult->HighRiskEnzymes_Recommendation = "";
						break;						
				}
				
				switch ($lac3) {
					case ($lac3 <= $lac1*1.15 && $lac3 >= $lac1*0.9):
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Regular'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.Slight'];
						$objResult->BleedingRisk_Recommendation = ""; 
						break;
					case ($lac3 <= $lac1*1.35 && $lac3 > $lac1*1.15):
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Regular'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.Medium'];
						$objResult->BleedingRisk_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC3.Recommendation']);
						break;
					case ($lac3 <= $lac1*1.6 && $lac3 > $lac1*1.35):
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Regular'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.High'];
						$objResult->BleedingRisk_Recommendation = utf8_encode($a_languages['Measurement.Answers.LAC3.Recommendation']);						
						break;						
					case ($lac3 >= $lac1*0.6 && $lac3 < $lac1*0.9):
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Medium'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.Low'];
						$objResult->BleedingRisk_Recommendation = "";
						break;
					case ($lac3 >= $lac1*0.1 && $lac3 < $lac1*0.6):
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Good'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.Low'];
						$objResult->BleedingRisk_Recommendation = "";
						break;						
					default:
						$objResult->MuscleRecovery = $a_languages['Measurement.Answers.LAC3.Error'];
						$objResult->BleedingRisk = $a_languages['Measurement.Answers.LAC3.Error'];
						$objResult->BleedingRisk_Recommendation = "";
						break;
				}
				
				$fr = $rsMeasurement->fields('respiratoryRate3');
				
				switch ($fr) {
					case ($fr < 12 && $fr > 70):
						$objResult->RespiratoryCompromise = utf8_encode($a_languages['Measurement.Answers.FR.Error']);
						break;
					case ($fr >= 12 && $fr < 30):
						$objResult->RespiratoryCompromise = $a_languages['Measurement.Answers.FR.Acceptable'];
						break;
					case ($fr >= 30 && $fr < 45):
						$objResult->RespiratoryCompromise = $a_languages['Measurement.Answers.FR.Slight'];
						break;
					case ($fr >= 45 && $fr <= 70):
						$objResult->RespiratoryCompromise = $a_languages['Measurement.Answers.FR.Marc'];
						break;
				}
			}
			
			$strSQL = "UPDATE measurement_item SET ";
			$strSQL .= "	resultValues = '".mysql_real_escape_string(json_encode($objResult))."' ";
			$strSQL .= " WHERE idMeasurementItem = " . $idMeasurementItem;
			$DB->Execute($strSQL);				
			
			$arrJSON->status = "OK";
			$arrJSON->idHorse = $rsMeasurement->fields('idHorse');
			$arrJSON->resultValues = $objResult;
			
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			$arrJSON->status = "ERROR";
			$arrJSON->idHorse = "0";
			$arrJSON->resultValues = "";
			return json_encode($arrJSON);
		}
	}
	
	function getPhrases()
	{
		include_once("includes/mcrypt.php");
		include_once("includes/constants.php");
		
		try
		{		
			$DB = NewADOConnection('mysqlt');
			$DB->Connect();
			$DB->Execute("SET NAMES utf8;");
			//$DB->debug=true;
			
			switch ($_REQUEST['language']) {
				case "es":
				case "en":
					$language = $_REQUEST['language'];
					break;
				default:
					$language = "es";
					break;
			}
			
			$strSQL = "SELECT * FROM phrases WHERE state <> '$stateDeleted' AND language = '$language'";
			$rsPhrases = $DB->Execute($strSQL);
			
			$i = 0;
			if($rsPhrases->EOF)
				return json_encode(array());
			else {
				while(!$rsPhrases->EOF)
				{
					$arrJSON[$i]['idPhrase'] = $rsPhrases->fields("idPhrase");
					$arrJSON[$i]['description'] = $rsPhrases->fields("description");
					$arrJSON[$i]['state'] = $rsPhrases->fields("state");
					$arrJSON[$i++]['language'] = $rsPhrases->fields("language");
					
					$rsPhrases->MoveNext();
				}
			}
			return json_encode($arrJSON);
		}
		catch (Exception $e)
		{
			return json_encode(array());
		}
	}
	
	header('Content-Type: application/json');
	switch($_REQUEST['action'])
	{
		case "validateUser":
			echo validateUser();
			break;
		case "getHorses":
			echo getHorses();
			break;
		case "setHorses":
			echo setHorses();
			break;
		case "uploadMeasurement":
			echo uploadMeasurement();
			break;
		case "getMeasurements":
			echo getMeasurements();
			break;
		case "getResultValues":
			echo getResultValues();
			break;
		case "getPhrases":
			echo getPhrases();
			break;			
	}
?>